var express = require('express');
var router = express.Router();
const TopoLayout = require('../models/TopoLayout');
var tools = require('../utils/tools')

router.get('/:ref', function (req, res, next) {
  TopoLayout.findOne({ ref: req.params.ref }, (err, doc) => {
    if (err) return tools.handleError(res, err);
    res.json({ status: 'success', data: doc })
  })
});

router.put('/:ref', function (req, res, next) {
  const { data } = req.body
  TopoLayout.findOneAndUpdate({ ref: req.params.ref }, { $set: { ref: req.params.ref, data } }, { upsert: true }, (err, doc) => {
    if (err) return tools.handleError(res, err);
    res.json({ status: 'success', data: doc })
  })
});

module.exports = router;
